package com.lai.qq.admin.modules.system.service.impl;

import java.util.List;

import com.lai.qq.admin.modules.system.mapper.UserRoleMapper;
import com.lai.qq.admin.modules.system.service.IUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lai.qq.admin.modules.system.dto.input.UserRoleQueryPara;
import com.lai.qq.admin.modules.system.entity.UserRole;

/**
 * <p>
 * 系统管理 - 用户角色关联表 服务实现类
 * </p>
 *
 * @author: zhengqing
 * @date: 2019-08-20
 */
@Service
@Transactional
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public void listPage(IPage<UserRole> page, UserRoleQueryPara filter) {
        page.setRecords(userRoleMapper.selectUserRoles(page, filter));
    }

    @Override
    public List<UserRole> list(UserRoleQueryPara filter) {
        return userRoleMapper.selectUserRoles(filter);
    }

    @Override
    public Integer saveOrUpdateData(UserRole para) {
        if (para.getId() != null) {
            userRoleMapper.updateById(para);
        } else {
            userRoleMapper.insert(para);
        }
        return para.getId();
    }

    @Override
    public void saveUserRole(UserRoleQueryPara para) {
        Integer roleId = para.getRoleId();
        String userIds = para.getUserIds();
        userRoleMapper.deleteByRoleId(roleId);
        if (StringUtils.isNotBlank(userIds)) {
            String[] userIdArrays = userIds.split(",");
            if (userIdArrays.length > 0) {
                for (String userId : userIdArrays) {
                    UserRole userRole = new UserRole();
                    userRole.setRoleId(roleId);
                    userRole.setUserId(Integer.parseInt(userId));
                    userRoleMapper.insert(userRole);
                }
            }
        }
    }

}
